﻿using System;

namespace XD.Office
{
    /// <summary>
    /// Xls模版参数配置对象
    /// </summary>
    [Serializable]
    public class TemplateParams
    {
        public TemplateParams()
        {
            TemplateFile = "";
            StartRow = 2;
            StartColumn = 1;
            MaxRows = 5000;
            Columns = null;
            ReplaceRule = "[{0}]";
            TableIndex = 1;
        }
        /// <summary>
        /// 模版文件路径
        /// 注：配置文件与模版文件同一目录,或者是绝对路径、应用程序相对路径
        /// </summary>
        public string TemplateFile { get; set; }
        /// <summary>
        /// 数据表位置（从1开始）。
        /// 对于Excel模版，TableIndex表示第几个WorkSheet
        /// 对应Word模版，TableIndex表示第几个表格
        /// </summary>
        public int TableIndex { get; set; }
        /// <summary>
        /// 数据区开始行号
        /// </summary>
        public int StartRow { get; set; }
        /// <summary>
        /// 数据区开始列号
        /// </summary>
        public int StartColumn { get; set; }
        /// <summary>
        /// 数据表最大行数
        /// </summary>
        public int MaxRows { get; set; }
        /// <summary>
        /// 列名称
        /// </summary>
        public string[] Columns { get; set; }
        /// <summary>
        /// 替换规则（比如  [{0}]   {0}为字段名称大写)
        /// </summary>
        public string ReplaceRule { get; set; }

        /// <summary>
        /// 从本地文件读取参数
        /// </summary>
        /// <param name="serFile">本地配置文件</param>
        public static TemplateParams ReadFromFile(string serFile)
        {
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(TemplateParams));
            System.IO.FileStream fs = null;
            TemplateParams par = null;
            try
            {
                fs = System.IO.File.Open(serFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                object instance = ser.Deserialize(fs);
                par = instance as TemplateParams;

                //转换路径
                if (!string.IsNullOrEmpty(par.TemplateFile))
                {
                    par.TemplateFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(serFile), par.TemplateFile);
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            return par;
        }
        /// <summary>
        /// 将配置保存到本地文件
        /// </summary>
        /// <param name="saveFile"></param>
        public void SaveToFile(string saveFile)
        {
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(TemplateParams));
            System.IO.FileStream fs = null;
            try
            {
                fs = System.IO.File.Open(saveFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                System.Xml.Serialization.XmlSerializerNamespaces xnsp = new System.Xml.Serialization.XmlSerializerNamespaces();
                xnsp.Add(string.Empty, string.Empty);
                ser.Serialize(fs, this, xnsp);
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }
    }
}
